<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="教练名称" prop="coachName">
        <el-input
          v-model="queryParams.coachName"
          placeholder="请输入教练名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教练性别" prop="coachSex">
        <el-select v-model="queryParams.coachSex" placeholder="请选择教练性别" clearable>
          <el-option
            v-for="dict in dict.type.coach_sex"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="教练手机号" prop="coachPhone">
        <el-input
          v-model="queryParams.coachPhone"
          placeholder="请输入教练手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['coach:coachmanagement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['coach:coachmanagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['coach:coachmanagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="coachmanagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="教练邀请码" align="center" prop="coachCode" />
      <el-table-column label="教练名称" align="center" prop="coachName" />
      <el-table-column label="教练性别" align="center" prop="coachSex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.coach_sex" :value="scope.row.coachSex"/>
        </template>
      </el-table-column>
      <el-table-column label="教练年龄" align="center" prop="coachAge" >
        <template slot-scope="{ row }">
          {{ row.coachAge }} 岁
        </template>
      </el-table-column>
      <el-table-column label="教练手机号" align="center" prop="coachPhone" />
      <el-table-column label="教练授课次数
" align="center" prop="coachTeachingTimes" >
        <template slot-scope="{ row }">
          {{ row.coachTeachingTimes }} 次
        </template>
      </el-table-column>
      <el-table-column label="教练个人信息" align="center" prop="coachInformation" />
<!--      <el-table-column label="教练特长
" align="center" prop="coachSpecialty">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.coach_specialty" :value="scope.row.coachSpecialty ? scope.row.coachSpecialty.split(',') : []"/>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['coach:coachmanagement:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['coach:coachmanagement:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教练信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules"  label-width="120px">
        <div style="width: 100%;margin-left: 15px;">
          <el-form-item label="教练图片" prop="coachImg" style="display: flex; align-items: center;">
            <image-upload v-model="form.coachImg" style="margin-left: -120px;" />
          </el-form-item>
        </div>
        <div style="display: flex;">
          <div style="width: 42%; margin-left: 15px;">
            <!-- Left side form items -->
            <el-form-item label="教练名称" prop="coachName"  >
              <el-input v-model="form.coachName" placeholder="请输入教练名称" style="width:240px;" />
            </el-form-item>
            <el-form-item label="教练性别" prop="coachSex">
              <el-radio-group v-model="form.coachSex">
                <el-radio :label="1">男</el-radio>
                <el-radio :label="2">女</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="教练年龄" prop="coachAge">
              <el-input v-model="form.coachAge" placeholder="请输入教练年龄" style="width:240px;" />
            </el-form-item>
            <el-form-item label="教练手机号" prop="coachPhone">
              <el-input v-model="form.coachPhone" placeholder="请输入教练手机号" style="width:240px;" />
            </el-form-item>
            <el-form-item label="教练密码" v-if="showmima" prop="coachPassword">
              <el-input v-model="form.coachPassword" placeholder="请输入教练密码" style="width:240px;" />
            </el-form-item>
          </div>
          <div style="width: 42%;">
            <!-- Right side form items -->
            <el-form-item label="教练个人信息" prop="coachInformation">
              <el-input v-model="form.coachInformation" placeholder="请输入教练个人信息" style="width:240px;" />
            </el-form-item>
            <el-form-item label="教练特长" prop="coachSpecialty">
              <el-select v-model="form.coachSpecialty" placeholder="请选择教练特长" multiple clearable
                         style="width:240px;">
                <el-option v-for="(item, index) in dict.type.coach_specialty" :key="index" :label="item.label"
                           :value="item.value" :disabled="item.disabled"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="教练经历" prop="coachExperience" style="height: 150px;">
              <el-input type="textarea" v-model="form.coachExperience" placeholder="请输入教练经历"  style="width: 240px;" :rows="9"  />
            </el-form-item>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCoachmanagement, getCoachmanagement, delCoachmanagement, addCoachmanagement, updateCoachmanagement } from "@/api/coach/coachmanagement";

export default {
  name: "Coachmanagement",
  dicts: ['coach_sex', 'coach_specialty'],
  data() {
    return {
      // 教练密码框 添加时显示 修改时隐藏
      showmima: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 教练信息表格数据
      coachmanagementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coachName: null,
        coachSex: null,
        coachPhone: null,
      },
      // 表单参数
      form: {

      },
      // 表单校验
      rules: {
        coachName: [
          { required: true, trigger: "blur", message: "请输入教练名称" }
        ],
        coachSex: [
          { required: true, trigger: "blur", message: "请选择教练性别" }
        ],
        coachPhone: [
          { required: true,pattern: /^[1][3-9]\d{9}$/, message: '请输入正确的手机号格式', trigger: 'blur' }
        ],
        coachPassword: [
          { required: true, message: '请输入教练密码', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]+$/, message: '密码必须是字母和数字', trigger: 'blur' }
        ],
        coachSpecialty: [
          { required: true, trigger: "blur", message: "请输入教练特长" }
        ],
        coachAge: [
          { required: true, trigger: "blur", message: "请输入教练年龄" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询教练信息列表 */
    getList() {
      this.loading = true;
      listCoachmanagement(this.queryParams).then(response => {
        this.coachmanagementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        venueUniqueIdentifier: null,
        coachCode: null,
        coachName: null,
        coachAge: null,
        coachSex: null,
        coachPhone: null,
        coachPassword: null,
        coachImg: null,
        coachTeachingTimes: null,
        coachInformation: null,
        coachSpecialty: [],
        coachExperience: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.showmima = true;
      this.title = "添加教练信息";
      this.form.coachSex = 1;
      this.open = true; // 打开弹窗
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.showmima = false;
      const id = row.id || this.ids
      getCoachmanagement(id).then(response => {
        this.form = response.data;
        this.form.coachSpecialty = this.form.coachSpecialty.split(",");
        this.open = true;
        this.title = "修改教练信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.coachSpecialty = this.form.coachSpecialty.join(",");
          if (this.form.id != null) {
            updateCoachmanagement(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(() => {
              // 显示错误信息
              this.$modal.msgError("修改失败，请检查输入信息并重试。");
            });
          } else {
            addCoachmanagement(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(() => {
              // 显示错误信息
              this.$modal.msgError("新增失败，请检查输入信息并重试。");
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除教练信息编号为"' + ids + '"的数据项？').then(function() {
        return delCoachmanagement(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('coach/coachmanagement/export', {
        ...this.queryParams
      }, `coachmanagement_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
